<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="userForm" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userForm.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="userForm.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="userForm.mobile" :disabled="!!id" maxlength="11" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <template>
                  <selectTree v-model="userForm.departmentId" />
                </template>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userForm.formOfEmployment" placeholder="请选择" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userForm.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker v-model="userForm.correctionTime" size="mini" type="date" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <!-- <el-upload
                  class="avatar-uploader"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                >
                  <img v-if="userForm.staffPhoto" :src="userForm.staffPhoto" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload> -->
                <uploadPhoto v-model="userForm.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="submit">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from './selectTree.vue'
import uploadPhoto from './uploadPhoto.vue'
import { getDepartments } from '@/api/department'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
export default {
  components: { selectTree, uploadPhoto },

  data() {
    return {
      // 放置表单数据
      userForm: {
        username: '', // 姓名
        workNumber: '', // 工号
        mobile: '', // 手机
        departmentId: null, // 部门
        formOfEmployment: '', // 聘用形式
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: null
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机格式不正确', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            // 转正时间不能小于入职时间 用时间戳判断
            if (new Date(value).getTime() < new Date(this.userForm.timeOfEntry).getTime()) {
              callback(new Error('转正时间不能小于入职时间'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      },
      id: null
    }
  },
  async created() {
    this.getDepartmentsFn()
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      const data = await getEmployeeDetail(this.id)
      this.userForm = data
      // console.log(data)
    }
  },
  methods: {
    async getDepartmentsFn() {
      this.options = await getDepartments()
    //   console.log(this.options)
    },
    async submit() {
      if (!this.id) {
        await addEmployee(this.userForm)
      } else {
        await updateEmployee(this.userForm)
      }
      this.$router.push('/employee')
    },
    handleAvatarSuccess(res, file) {
      this.userForm.staffPhoto = URL.createObjectURL(file.raw)
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
    background: #fff;
    padding: 20px;

    .inputW {
        width: 380px
    }

}
</style>
